package cn.tedu.knows.portal.service.impl;

import cn.tedu.knows.portal.exception.ServiceException;
import cn.tedu.knows.portal.mapper.QuestionMapper;
import cn.tedu.knows.portal.mapper.UserMapper;
import cn.tedu.knows.portal.model.Answer;
import cn.tedu.knows.portal.mapper.AnswerMapper;
import cn.tedu.knows.portal.model.Question;
import cn.tedu.knows.portal.model.User;
import cn.tedu.knows.portal.service.IAnswerService;
import cn.tedu.knows.portal.vo.AnswerVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author tedu.cn
 * @since 2021-07-23
 */
@Service
public class AnswerServiceImpl extends ServiceImpl<AnswerMapper, Answer> implements IAnswerService {

    @Resource
    private UserMapper userMapper;

    @Resource
    private AnswerMapper answerMapper;

    @Override
    @Transactional
    public Answer saveAnswer(AnswerVo answerVo, String username) {
        User user=userMapper.findUserByUsername(username);
        Answer answer=new Answer()
                .setQuestId(answerVo.getQuestionId())
                .setContent(answerVo.getContent())
                .setUserId(user.getId())
                .setUserNickName(user.getNickname())
                .setCreatetime(LocalDateTime.now())
                .setLikeCount(0)
                .setAcceptStatus(0);
        int num=answerMapper.insert(answer);
        if(num!=1){
            throw new ServiceException("数据库异常");
        }
        //千万别忘了返回
        return answer;
    }

    @Override
    public List<Answer> getQuestionAnswers(Integer questionId) {

        List<Answer> answers=answerMapper
                .findAnswersWithCommentByQuestionId(questionId);
        //别忘了返回
        return answers;
    }

    @Resource
    private QuestionMapper questionMapper;
    @Override
    //连续的两个修改操作,必须添加事务的支持
    @Transactional
    public boolean accept(Integer answerId, String username) {
        User user=userMapper.findUserByUsername(username);
        //查询answer对象
        Answer answer=answerMapper.selectById(answerId);
        //回答对应的问题对象
        Question question=questionMapper
                                .selectById(answer.getQuestId());
        // 判断当前采纳回答的学生是不是问题的发布者
        if(user.getId().equals(question.getUserId())){
            //修改回答状态
            int num=answerMapper.updateAcceptStatus(
                                        answerId,1);
            if(num!=1){
                throw new ServiceException("数据库异常");
            }
            num=questionMapper.updateStatus(
                    answer.getQuestId(),Question.SOLVED);
            if(num!=1){
                throw new ServiceException("数据库异常");
            }
            //别忘了返回!!!
            return true;
        }

        return false;
    }
}
